local xuanbian = fk.CreateSkill {
  name = "xiaobai__xuanbian"
}
Fk:loadTranslationTable {
  ["xiaobai__xuanbian"] = "玄辩",
  [":xiaobai__xuanbian"] = "每轮每项限一次，其他角色使用“诂守”被删去项牌后，你可以与其依次展示一张手牌：" ..
      "展示牌为“诂守”当前首项者获得一张【影】，" ..
      "展示牌与使用牌类型相同者摸一张牌，然后你弃置K点的展示牌并重置“诂守”。",
  ["#xiaobai__xuanbian"] = "是否发动 玄辩？与 %dest 依次展示一张手牌",
  ["#xiaobai__xuanbian-cards"] = "玄辩：%dest 展示了一张 %arg ，请你展示一张手牌",

  ["xiaobai__xuanbian_damage"] = "伤害牌",
  ["xiaobai__xuanbian_trick"] = "锦囊牌",
  ["xiaobai__xuanbian_K"] = "K"
}
local U = require "packages.utility.utility"

xuanbian:addEffect(fk.CardUseFinished, {
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(xuanbian.name) and target and target ~= player and data
        and not player:isKongcheng() and not target:isKongcheng() then
      local num = player:getMark("xiaobai__gushou")
      local choices = player:getTableMark("@xiaobai__xuanbian-turn")
      if num > 0 and data.card.is_damage_card then
        return not table.contains(choices, "xiaobai__xuanbian_damage")
      end
      if num > 1 and data.card.type == Card.TypeTrick then
        return not table.contains(choices, "xiaobai__xuanbian_trick")
      end
      if num > 2 and data.card.number == 13 then
        return not table.contains(choices, "xiaobai__xuanbian_K")
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local cards = player.room:askToCards(player, {
      include_equip = false,
      cancelable = true,
      skill_name = xuanbian.name,
      max_num = 1,
      min_num = 1,
      pattern = ".|.|.|hand",
      prompt = "#xiaobai__xuanbian::" .. target.id
    })
    if #cards > 0 then
      event:setCostData(self, { cards = cards })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards1 = event:getCostData(self).cards
    player:showCards(cards1)
    local cards2 = room:askToCards(target, {
      include_equip = false,
      cancelable = false,
      skill_name = xuanbian.name,
      max_num = 1,
      min_num = 1,
      pattern = ".|.|.|hand",
      prompt = "#xiaobai__xuanbian-cards::" .. player.id .. ":" .. Fk:getCardById(cards1[1]):toLogString()
    })
    target:showCards(cards2)
    local card1, card2 = Fk:getCardById(cards1[1]), Fk:getCardById(cards2[1])
    local num = player:getMark("xiaobai__gushou")
    if num == 0 then
      if card1.is_damage_card then
        room:moveCards({
          ids = U.getShade(room, 1),
          to = player,
          toArea = Card.PlayerHand,
          moveReason = fk.ReasonJustMove,
          proposer = player,
          skillName = xuanbian.name,
          moveVisible = true,
        })
      end
      if card2.is_damage_card then
        room:moveCards({
          ids = U.getShade(room, 1),
          to = target,
          toArea = Card.PlayerHand,
          moveReason = fk.ReasonJustMove,
          proposer = player,
          skillName = xuanbian.name,
          moveVisible = true,
        })
      end
    elseif num == 1 then
      if card1.type == Card.TypeTrick then
        room:moveCards({
          ids = U.getShade(room, 1),
          to = player,
          toArea = Card.PlayerHand,
          moveReason = fk.ReasonJustMove,
          proposer = player,
          skillName = xuanbian.name,
          moveVisible = true,
        })
      end
      if card2.type == Card.TypeTrick then
        room:moveCards({
          ids = U.getShade(room, 1),
          to = target,
          toArea = Card.PlayerHand,
          moveReason = fk.ReasonJustMove,
          proposer = player,
          skillName = xuanbian.name,
          moveVisible = true,
        })
      end
    elseif num == 2 then
      if card1.number == 13 then
        room:moveCards({
          ids = U.getShade(room, 1),
          to = player,
          toArea = Card.PlayerHand,
          moveReason = fk.ReasonJustMove,
          proposer = player,
          skillName = xuanbian.name,
          moveVisible = true,
        })
      end
      if card2.number == 13 then
        room:moveCards({
          ids = U.getShade(room, 1),
          to = target,
          toArea = Card.PlayerHand,
          moveReason = fk.ReasonJustMove,
          proposer = player,
          skillName = xuanbian.name,
          moveVisible = true,
        })
      end
    end

    if card1.type == data.card.type then player:drawCards(1, xuanbian.name) end
    if card2.type == data.card.type then target:drawCards(1, xuanbian.name) end

    local moveInfos = {}
    if card1.number == 13 then
      table.insert(moveInfos, {
        from = player.id,
        ids = cards1,
        toArea = Card.DiscardPile,
        moveReason = fk.ReasonDiscard,
        proposer = player,
        skillName = xuanbian.name,
      })
    end
    if card2.number == 13 then
      table.insert(moveInfos, {
        from = target.id,
        ids = cards2,
        toArea = Card.DiscardPile,
        moveReason = fk.ReasonDiscard,
        proposer = player,
        skillName = xuanbian.name,
      })
    end
    if #moveInfos > 0 then
      room:moveCards(table.unpack(moveInfos))
      room:setPlayerMark(player, "xiaobai__gushou", 0)
      room:setPlayerMark(player, "xiaobai__gushou_number", 0)
    end
    if data.card.is_damage_card then
      room:addTableMarkIfNeed(player, "@xiaobai__xuanbian-turn", "xiaobai__xuanbian_damage")
    end
    if num > 1 and data.card.type == Card.TypeTrick then
      room:addTableMarkIfNeed(player, "@xiaobai__xuanbian-turn", "xiaobai__xuanbian_trick")
    end
    if num > 2 and data.card.number == 13 then
      room:addTableMarkIfNeed(player, "@xiaobai__xuanbian-turn", "xiaobai__xuanbian_K")
    end
  end,
})

return xuanbian
